package com.coin900.coin;


import com.coin900.coin.base.Constants;
import com.coin900.coin.base.FuConstants;
import com.coin900.coin.base.utils.JwtUtils;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.cmd.Scheduler;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.service.sbi.ISbiSystemConfigService;
import io.jsonwebtoken.Claims;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by laborc on 2017/4/12.
 */
@RestController
public class HttpHandlerCtrl  implements InitializingBean {
    private final static Logger logger = LoggerFactory.getLogger(HttpHandlerCtrl.class);
    private ExecutorService executorService = Executors.newFixedThreadPool(5);


    @RequestMapping(path = "/api/web", method = RequestMethod.POST)
    @ResponseBody
    String getWebServer(@RequestBody String body, HttpServletRequest request) {
        try {
            JSONObject cmdJson = JSONObject.fromObject(body);
            CmdSign cmdSign = (CmdSign) JSONObject.toBean(cmdJson, CmdSign.class);
            return this.execute(cmdSign, request).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return "";
    }

    @RequestMapping(path = "/api/web", method = RequestMethod.GET)
    @ResponseBody
    String getWebServer2(HttpServletRequest request) {
        System.out.println("213123");
        return "21312321221121";
    }

    private CmdSign execute(CmdSign cmdSign, HttpServletRequest request) {
        CmdSign respSign = null;
        try {
            String cmdCode = cmdSign.getCmdCode();
            if(!(cmdCode.equals(CmdCode.FU_LOGIN)||cmdCode.equals(CmdCode.SESSION_GET))){
                String sessionToken = cmdSign.getSessionToken();
                Claims claims=null;
                try {
                    claims = JwtUtils.checkJWT(sessionToken, Constants.jwt_base64Security);
                }catch (Exception e){
                    if (cmdSign != null) {
                        respSign = new CmdSign(cmdSign);
                        respSign.setRespCode(RespCode.LOGIN_FAIL);
                        respSign.setMsg(e.getMessage());
                    }
                }
                Long userId = Long.parseLong(claims.get("userId").toString());
                cmdSign.setUserId(userId);
            }
            respSign = this.scheduler.schedule(request, cmdSign);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            if (cmdSign != null) {
                respSign = new CmdSign(cmdSign);
                respSign.setRespCode(RespCode.FAIL);
                respSign.setMsg(e.getMessage());
            }
        }
        return respSign;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        FuConstants.ACCESS_KEY = "7JzlGgmsG0Idal2ywhU0huFtDH-8dINGLdJROfEA";
        FuConstants.SECRET_KEY = "xj_Cv1IzUaTWzSCqWYNkCCe1_N9T6_kUt0lJQc-E";
        FuConstants.BUCKET_NAME = "xchat-dev";
        FuConstants.BUCKET_DOMAIN = "http://op007tqer.bkt.clouddn.com";

        FuConstants.P_ACCESS_KEY = "7JzlGgmsG0Idal2ywhU0huFtDH-8dINGLdJROfEA";
        FuConstants.P_SECRET_KEY = "xj_Cv1IzUaTWzSCqWYNkCCe1_N9T6_kUt0lJQc-E";
        FuConstants.P_BUCKET_NAME = "xchat-p";
        FuConstants.P_BUCKET_DOMAIN = "http://otq48t1yh.bkt.clouddn.com";
    }


    @Autowired
    private Scheduler scheduler;

    @Autowired
    private ISbiSystemConfigService systemConfigService;
}
